
//点击按钮发送请求

function getLeaveInfomation() {
    var leave_info = document.querySelectorAll('#leave-info')
    return leave_info
}
function getAdjustInfomation() {
    var adjust_info = document.querySelectorAll('#adjust-info')
    return adjust_info
}
var leave_count = 0,
    adjust_count = 0
var pattern = /^\d+-\d+-\d+$/
var dialog = document.getElementsByTagName("dialog")[0],
    openDialog = document.getElementsByClassName("leave_btn"),
    closeDialog = document.getElementById("close_dialog");
openDialog[0].addEventListener('click', function () {
    getLeaveInfomation()
    console.log(leave_info);
    for (let i = 0; i < leave_info.length; i++) {
        console.log(leave_info[i].value);
        if (leave_info[i].value != '') {
            if (i == leave_info.length - 1 && pattern.test(leave_info[4].value)) {
                dialog.children[0].textContent = '请假成功'
                //发送请求
                axios({
                    url: 'http://frp-fly.top:54242/SLS/LeaveServlet',
                    method: 'post',
                    data: {
                        "name": `${leave_info[0].value}`,
                        "gender": `${leave_info[1].value}`,
                        "direction": `${leave_info[2].value}`,
                        "term": `${leave_info[3].value}`,
                        "leave_date": `${leave_info[4].value}`,
                        "leave_class": `${leave_info[5].value}`,
                        "reason": `${leave_info[6].value}`
                    }
                }).then(res => {
                    console.log(res.data)
                })
                break
            } else {
                dialog.children[0].textContent = '请按照正确格式输入'
            }
        } else {
            dialog.children[0].textContent = '请将信息填写完整'
        }
    }
    dialog.showModal();
})
openDialog[1].addEventListener('click', function () {
    getAdjustInfomation()
    console.log(adjust_info);
    for (let i = 0; i < adjust_info.length; i++) {
        if (adjust_info[i].value != '') {
            if (i == adjust_info.length - 1 && (pattern.test(adjust_info[4].value) && pattern.test(adjust_info[6].value))) {
                dialog.children[0].textContent = '申请成功'
                //发送请求
                axios({
                    url: 'http://frp-fly.top:54242/SLS/AdjustServlet',
                    method: 'post',
                    data: {
                        "name": `${adjust_info[0].value}`,
                        "gender": `${adjust_info[1].value}`,
                        "direction": `${adjust_info[2].value}`,
                        "term": `${adjust_info[3].value}`,
                        "leave_date": `${adjust_info[4].value}`,
                        "leave_class": `${adjust_info[5].value}`,
                        "adjust_date": `${adjust_info[6].value}`,
                        "adjust_class": `${adjust_info[7].value}`,
                        "reason": `${adjust_info[8].value}`
                    }
                }).then(res => {
                    console.log(res.data)
                })
                break
            } else {
                dialog.children[0].textContent = '请按照正确格式输入'
            }
        }
        else {
            dialog.children[0].textContent = '请将信息填写完整'

        }
    }
    dialog.showModal();


})

closeDialog.onclick = function () {
    dialog.close();
}


//获取请假次数
function getAccount() {
    var leave_record = document.getElementById('leave_record')
    axios.defaults.baseURL = 'http://frp-fly.top:54242/SLS/'
    axios({
        method: "post",
        url: "/LeaveAccountServlet",
        headers: {
            "Content-type": "application/json"
        }
    }).then(res => {
        console.log(res.data)
        var count = res.data.data
        console.log(count);
        leave_record.children[1].textContent = '已请假' + `${res.data.data}` + '次'
    })


    var adjust_record = document.getElementById('adjust_record')
    axios({
        method: "post",
        url: "/AdjustAccountServlet",
        headers: {
            "Content-type": "application/json"
        }
    }).then(res => {
        console.log(res.data)
        var count = res.data.data
        console.log(count);
        adjust_record.children[1].textContent = '已调整' + `${res.data.data}` + '次'
    })
}


//查看历史记录
var wrapper = document.getElementsByClassName('wrapper')
var leave_log = document.getElementById('leave_log')
var adjust_log = document.getElementById('adjust_log')
var back_btn = document.getElementsByClassName('back_btn')
var content = document.querySelectorAll('.content')
var content_hidden = document.querySelector('.content_hidden')
leave_log.addEventListener('click', () => {
    wrapper[0].style.display = 'block'
    content[0].style.display = 'none'
    getRecord()
})
back_btn[0].addEventListener('click', () => {
    wrapper[0].style.display = 'none'
    content[0].style.display = 'block'
    getAccount()
})


adjust_log.addEventListener('click', () => {
    wrapper[1].style.display = 'block'
    content[1].style.display = 'none'
    getRecord()
})
back_btn[1].addEventListener('click', () => {
    wrapper[1].style.display = 'none'
    content[1].style.display = 'block'
    getAccount()
})

//获取登录用户方向-期数-姓名信息
function getInfomation() {
    var user_infomation = document.getElementById('user_infomation')
    axios({
        method: "post",
        url: "http://frp-fly.top:54242/SLS/inforServlet",
        headers: {
            "Content-type": "application/json"
        }
    }).then(res => {
        console.log(res.data.data)
        user_infomation.textContent = `${res.data.data}`
    })
}

getAccount()
getInfomation()
// setTimeout(function(){
//     },3000);